#include "DefaultState.hpp"
#include "../Notification.hpp"
#include <TweenEngine/Tween.h>
#include <cpp3ds/System/I18n.hpp>
#include <cpp3ds/Window/Window.hpp>

namespace Template {

DefaultState *g_defaultState = nullptr;

DefaultState::DefaultState(StateStack& stack, Context& context, StateCallback callback)
: State(stack, context, callback)
{
	// State constructor, initialize everything here
	g_defaultState = this;

	// Let's setup a text, set a string to the text like that
	m_helloWorldText.setString("Hello world!");
	// The text that it will be using the system font
	m_helloWorldText.useSystemFont();
	// The text will have a character size of 24
	m_helloWorldText.setCharacterSize(24);
	// You can set the fill color of a text, as well as it's outline color using a cpp3ds::Color
	m_helloWorldText.setFillColor(cpp3ds::Color(80, 80, 80, 255));
	// Just position the text so it is on the middle of the screen
	m_helloWorldText.setPosition(200.f, 120.f);
	// And set the origin of the text to the center of the text, this origin will be the reference for translations / rescalings...
	m_helloWorldText.setOrigin(m_helloWorldText.getLocalBounds().width / 2, m_helloWorldText.getLocalBounds().height / 2);

	// Using the tween engine, apply a rescaling animation to the text that will be repeated indefinitly
	TweenEngine::Tween::to(m_helloWorldText, m_helloWorldText.SCALE_XY, 4.f)
		.target(0.9f, 0.9f)
		.repeatYoyo(-1, 0.f)
		.start(m_tweenManager);

	// Let's setup another text that will be drawn on the bottom screen
	m_complexText.setPosition(5.f, 5.f);
	m_complexText.setCharacterSize(12);
	// cpp3ds::Color has presets, like cpp3ds::Color::Red, ::Green, ::Blue, ::Black, ::White and ::Transparent
	m_complexText.setFillColor(cpp3ds::Color::Green);
	// Setup an outline for the text, you need to supply the outline's color and thickness
	m_complexText.setOutlineColor(cpp3ds::Color::Red);
	m_complexText.setOutlineThickness(5);
	// You can use the _() function for the I18n manager and to format a string like a printf !
	m_complexText.setString(_("This string is from the _() function\nThis is a number: %i", 2));
	// You can apply a style to your text and even combine them, here's an example:
	m_complexText.setStyle(cpp3ds::Text::Italic | cpp3ds::Text::Underlined);
}

DefaultState::~DefaultState()
{

}

void DefaultState::renderTopScreen(cpp3ds::Window& window)
{
	// This will draw the text "Hello world!" previously setupped on the top screen
	window.draw(m_helloWorldText);
}

void DefaultState::renderBottomScreen(cpp3ds::Window& window)
{
	// We are drawing the complex text on the bottom screen, simple !
	window.draw(m_complexText);
}

bool DefaultState::update(float delta)
{
	// Always update the tween manager if you have one in your class
	m_tweenManager.update(delta);

	return true;
}

bool DefaultState::processEvent(const cpp3ds::Event& event)
{
	// In this section, you can a lot of event, let's see how this can be done

	// First of all, you have to check your event type
	if (event.type == cpp3ds::Event::KeyPressed) {
		// Here, we can handle an event when a key is pressed, you can do a switch() to do an action according to the key pressed
		switch (event.key.code) {
			case cpp3ds::Keyboard::Start: {
				// Exit the app if the user pressed the Start button
				requestStackClear();
				return true;
			}
			case cpp3ds::Keyboard::A: {
				// For example, when the user press the A button, you can change a text
				/* Here, we are changing the text style:
				Using an AND operation, we are checking if the text has the Italic style
				If it has the Italic style, we remove it by getting the style and XOR it with cpp3ds::Text::Italic
				If it has NOT the Italic style, we add it by getting the style and OR it with cpp3ds::Text::Italic*/
				if (m_complexText.getStyle() & cpp3ds::Text::Italic)
					// Here, the Italic style is removed
					m_complexText.setStyle(m_complexText.getStyle() ^ cpp3ds::Text::Italic);
				else
					// Here, the Italic style is added
					m_complexText.setStyle(m_complexText.getStyle() | cpp3ds::Text::Italic);
			}
		}
	} else if (event.type == cpp3ds::Event::TouchBegan) {
		// Here, we can handle an event from the touch screen, we can acces the coordinate of the touch with event.touch.x and event.touch.y

		// A notification is just sent with the Notification.cpp file included in this pack, we can see again the utility of _() to format a string
		Notification::spawn(_("Touch event @ x:%i, y:%i", event.touch.x, event.touch.y));
	}

	return true;
}

} // namespace Template
